package cat.tom.controller;

import cat.tom.mapper.HeadlineMapper;
import cat.tom.pojo.Headline;
import cat.tom.service.HeadlineService;
import cat.tom.utils.JwtHelper;
import cat.tom.utils.Result;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author shkstart
 * @create 2024-03-11 3:42
 */
@RestController
@RequestMapping("headline")
@CrossOrigin
public class HeadLineController {

    @Autowired
    private JwtHelper jwtHelper;

    @Autowired
    private HeadlineService headlineService;

    @PostMapping("publish")
    public Result publish(@RequestBody Headline headline, @RequestHeader String token){

        Integer userId = jwtHelper.getUserId(token).intValue();
        headline.setPublisher(userId);
        Result result = headlineService.publish(headline, token);
        return result;

    }

    @PostMapping("findHeadlineByHid")
    public Result findHeadlineByHid(@Param("hid") Integer hid){
        Result result = headlineService.findHeadlineByHid(hid);
        return result;

    }

    @PostMapping("update")
    public Result update(@RequestBody Headline headline){

        Result result = headlineService.updateHeadline(headline);
        return result;

    }

    @PostMapping("removeByHid")
    public Result removeByHid(@Param("hid") Integer hid){

        headlineService.removeById(hid);
        return Result.ok(null);

    }
}
